from itsdangerous import TimedJSONWebSignatureSerializer, BadData

from meiduo import settings

# 创建加密方法　　　jws
def generate_access_token(openid):
    serializer = TimedJSONWebSignatureSerializer(settings.SECRET_KEY,3600)

    # serializer.dumps(数据),加密数据， 返回bytes类型
    token = serializer.dumps({'openid': openid})
    token = token.decode()
    # serializer.loads()  解密方法
    return token

# 创建解密方法
def check_save_user_token(openid):
    serializer = TimedJSONWebSignatureSerializer(settings.SECRET_KEY,3600)

    # serializer.loads() 解密
    try:
        data = serializer.loads(openid)
    except BadData:
        return None
    else:
        return data
